package at.codebase.web.servlets.user;

import static at.codebase.web.util.i18n.I18NFactory.i18n;

import java.util.ArrayList;

import at.codebase.core.RichtTextFormatter;
import at.codebase.db.dao.EUserDao;
import at.codebase.db.entities.EUser;
import at.codebase.security.SecurityUtils;
import at.codebase.web.templates.MainTemplate;
import at.codebase.web.templates.UserProfileTemplate;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters.ParameterException;
import at.codebase.web.util.i10n.L10N;
import at.codebase.web.util.servlet.SecureServlet;
import at.codebase.web.util.template.Template;
import at.codebase.web.util.template.TemplateEngine;

public class UserDetails extends SecureServlet {

	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ANONYMOUS);
		secLevels.add(UserLevel.REGISTERED);
		secLevels.add(UserLevel.ADMIN);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo,
			UserLevel level, RequestType requestType) {

		EUser user = null;
		try {
			long id = requestInfo.getParameters().getLong("userId");
			user = EUserDao.getUserById(new Long(id));
		} catch (ParameterException e) {
			if (requestInfo.isUserLoggedIn()) {
				user = EUserDao.getUserById(requestInfo.getUser().getId());
			} else {
				redirectTo("/newest");
			}
		}

		MainTemplate mainTemplate = new MainTemplate(requestInfo, writer());

		mainTemplate.addJSScript("script_user_profile.js");

		if (user != null) {
			// Check if User is active
			if (user.isActive()
			// If user is Admin
					|| level.equals(UserLevel.ADMIN)) {
				mainTemplate.setTitle(i18n(requestInfo.getLocale()).string(
						user.getUsername()));
				// Show the User Profile
				buildPage(mainTemplate, requestInfo, level, user);
			} else {
				// Show Not Found Error (Because User is not active)
				show404NotFound(mainTemplate, requestInfo);
			}
		} else {
			// Show Not Found Error
			show404NotFound(mainTemplate, requestInfo);
		}

		mainTemplate.render(requestInfo.getLocale());

	}
	
	private void buildPage(MainTemplate mainTemplate,
			RequestInformation requestInfo, UserLevel level, EUser user) {
		
		UserProfileTemplate userProfileTemplate = new UserProfileTemplate(requestInfo, level, user);
		
		Template userDetails = TemplateEngine.getTemplate("user/site_profile_details.html");
		userDetails.setText("userUsername", user.getUsername());
		
		String safeDescriptionText =  SecurityUtils.sanatize(user.getDescription());
		userDetails.setHTML("userDescription", RichtTextFormatter.toRichText(safeDescriptionText).replace("\n", "<br>"));

		userDetails.setHTML("userCreate",
				i18n(requestInfo.getLocale()).string(
						"userProfile_details_memberSince",
						L10N.formatDate(requestInfo.getLocale(),
								user.getTimeCreate())));
		
		userProfileTemplate.setContent(userDetails);

		mainTemplate.setTemplate("body", userProfileTemplate);
	}

	private void show404NotFound(MainTemplate mainTemplate,
			RequestInformation requestInfo) {
		mainTemplate.setTitle(i18n(requestInfo.getLocale()).string(
				"userProfile_error_notFound_title"));
		Template notFoundUserTemplate = TemplateEngine
				.getTemplate("user/error_not_found_user.html");
		mainTemplate.setTemplate("body", notFoundUserTemplate);
	}
}